/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package selling;

import java.io.*;
import java.util.ArrayList;
import selling.Form.SellingModule;
import java.util.Date;
/**
 *
 * @author Milewski, Iwicki
 */

public class FormImpl implements SellingModule {

        public class IO
    {
        public ArrayList<String> GetRecords(String filename)
        {
            ArrayList<String> temp = new ArrayList<String>();
            File file = new File("C:\\repo\\files\\" + filename);
            BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

           while ((text = reader.readLine()) != null)
           {
                temp.add(text);
           }
        } catch (Exception e)
        {
          return null;
        }
            return temp;
        }

        public Boolean AddRecord(String filename, String record)
        {
          try {
              FileWriter outFile = new FileWriter("C:\\repo\\files\\" + filename, true);
              PrintWriter out = new PrintWriter(outFile);

              out.println(record);
              out.close();
              return true;
          } catch (IOException e){
              e.printStackTrace();
              return false;
          }
        }

        public Boolean AddFirstRecord(String filename, String record)
        {
          try {
              FileWriter outFile = new FileWriter("C:\\repo\\files\\" + filename, false);
              PrintWriter out = new PrintWriter(outFile);

              out.println(record);
              out.close();
              return true;
          } catch (IOException e){
              e.printStackTrace();
              return false;
          }
        }
    }

    public ArrayList<String> GetClients()
    {
        IO robol = new IO();
        return robol.GetRecords("klienci.dat");
    }

    public ArrayList<String> GetTowary()
    {
        IO robol = new IO();
        return robol.GetRecords("towary.dat");
    }

    public Boolean SaveTowary(ArrayList<String> Towary)
    {
        IO robol = new IO();
        
        Boolean first = true;
        for (String towar : Towary) {
            if(first)
            {
                if(!robol.AddFirstRecord("towary.dat", towar)) return false;
                first = false;
            }
            else if(!robol.AddRecord("towary.dat", towar)) return false;
        }
        return true;
    }

       public Boolean SaveClients(ArrayList<String> Klienci)
       {
           IO robol = new IO();

        Boolean first = true;
        for (String towar : Klienci) {
            if(first)
            {
                if(!robol.AddFirstRecord("klienci.dat", towar)) return false;
                first = false;
            }
            else if(!robol.AddRecord("klienci.dat", towar)) return false;
        }
        return true;
       }


       public ArrayList<String> GetFactures()
       {
            IO robol = new IO();
            return robol.GetRecords("faktury.dat");
       }

       public Boolean SaveFactures(ArrayList<String> Faktury)
       {
           IO robol = new IO();

            Boolean first = true;
            for (String towar : Faktury) {
                if(first)
                {
                    if(!robol.AddFirstRecord("faktury.dat", towar)) return false;
                    first = false;
                }
                else if(!robol.AddRecord("faktury.dat", towar)) return false;
            }
            return true;
       }

       public ArrayList<String> GetAccounts()
       {
            IO robol = new IO();
            return robol.GetRecords("konta.dat");
       }

       public Boolean SaveAccounts(ArrayList<String> Konta)
       {
           IO robol = new IO();

            Boolean first = true;
            for (String towar : Konta) {
                if(first)
                {
                    if(!robol.AddFirstRecord("konta.dat", towar)) return false;
                    first = false;
                }
                else if(!robol.AddRecord("konta.dat", towar)) return false;
            }
            return true;
       }

       public Boolean SavePlan(String plan)
       {
            IO robol = new IO();

            if(!robol.AddRecord("planowanie.dat", plan)) return false;
            return true;
       }

       public ArrayList<String> GetPlans()
       {
           IO robol = new IO();
            return robol.GetRecords("planowanie.dat");
       }
}
